﻿using System;
using ChiWalker.Dynamics;
using ChiWalker.Helpers;
using ChiWalker.Spell;
using ChiWalker.Utilities;
using Styx;
using Styx.Common.Helpers;
using Styx.TreeSharp;

namespace ChiWalker.Routines.DeathKnight
{
    internal class Blood
    {
        private static readonly WaitTimer DiseaseTimer = new WaitTimer(TimeSpan.FromSeconds(0.5));

        [Behavior(BehaviorType.Combat | BehaviorType.Pull)]
        [Spec(WoWSpec.DeathKnightBlood)]
        [Class(WoWClass.DeathKnight)]
        public static Composite DeathKnightBlood()
        {
            return
                new PrioritySelector(
                    Safers.EnsureTarget(),
                    WaitFor.WaitForCast(),
                    Movement.MoveToLoS(ret => StyxWoW.Me.CurrentTarget),
                    Movement.FaceMyTarget(),
                    Safers.EnsureAttacking(),
                    new CastOnMyTarget(Spell.MindFreeze,
                        ret =>
                            StyxWoW.Me.CurrentTarget.IsCasting &&
                            StyxWoW.Me.CurrentTarget.CanInterruptCurrentSpellCast),
                    new CastOnMyTarget(Spell.Outbreak,
                        ret => DiseaseTimer.IsFinished &&
                               !StyxWoW.Me.CurrentTarget.HasMyAura("Frost Fever") &&
                               !StyxWoW.Me.CurrentTarget.HasMyAura("Blood Plague"),
                        ret => DiseaseTimer.Reset()),
                    new CastOnMyTarget(Spell.BloodBoil,
                        ret => DiseaseTimer.IsFinished &&
                               StyxWoW.Me.CurrentTarget.Distance <= 10f &&
                               (StyxWoW.Me.CurrentTarget.HasMyAura("Frost Fever") &&
                                StyxWoW.Me.CurrentTarget.GetAuraTimeLeft("Frost Fever", true)
                                    .TotalSeconds <= 3) ||
                               (StyxWoW.Me.CurrentTarget.HasMyAura("Blood Plague") &&
                                StyxWoW.Me.CurrentTarget.GetAuraTimeLeft("Blood Plague", true)
                                    .TotalSeconds <= 3), ret => DiseaseTimer.Reset()),
                    new CastAtLocation(Spell.DeathandDecay, ret => StyxWoW.Me.CurrentTarget.Location,
                        ret =>
                            StyxWoW.Me.CurrentTarget.Distance <= 20 &&
                            Clusters.GetClusterCount(StyxWoW.Me.CurrentTarget,
                                Clusters.NearbyUnitsInCombatWithMe, ClusterType.Radius,
                                10f) >= 3),
                    new CastAtLocation(Spell.DeathandDecay, ret => StyxWoW.Me.CurrentTarget.Location,
                        ret =>
                            StyxWoW.Me.HasAura("Crimson Scourge") && StyxWoW.Me.CurrentTarget.Distance <= 20),
                    new CastOnMyTarget(Spell.BloodBoil,
                        ret =>
                            StyxWoW.Me.HasAura("Crimson Scourge") && StyxWoW.Me.CurrentTarget.Distance <= 10f),
                    new CastOnMyTarget(Spell.IcyTouch,
                        ret =>
                            DiseaseTimer.IsFinished && !StyxWoW.Me.CurrentTarget.HasMyAura("Frost Fever"),
                        ret => DiseaseTimer.Reset(), true),
                    new CastOnMyTarget(Spell.PlagueStrike,
                        ret =>
                            DiseaseTimer.IsFinished && !StyxWoW.Me.CurrentTarget.HasMyAura("Blood Plague"),
                        ret => DiseaseTimer.Reset(), true),
                    new CastOnMyTarget(Spell.DeathStrike),
                    new CastOnSelf(Spell.BloodBoil,
                        ret =>
                            Clusters.GetClusterCount(StyxWoW.Me, Clusters.NearbyUnitsInCombatWithMe,
                                ClusterType.Radius, 10) >= 3),
                    new CastOnMyTarget(Spell.HeartStrike, ret => StyxWoW.Me.BloodRuneCount >= 1),
                    new CastOnMyTarget(Spell.BloodStrike, ret => StyxWoW.Me.BloodRuneCount >= 1),
                    new CastOnMyTarget(Spell.RuneStrike),
                    new CastOnMyTarget(Spell.DeathStrike),
                    new CastOnSelf(Spell.HornofWinter),
                    Movement.MoveToUnit(ret => StyxWoW.Me.CurrentTarget)
                    );
        }
    }
}